#include "sms.h"

#define TASKBAR_LAYOUT_NAME "layout-taskbar"

typedef enum _TASKBAR_MEMBER{
	TASKBAR_MEMBER_USB,
 TASKBAR_MEMBER_SD,
 TASKBAR_MEMBER_LAN,
 TASKBAR_MEMBER_WIFI,
 TASKBAR_MEMBER_COUNT,
}TASKBAR_MEMBER;

typedef enum _TICON_STATUS{
	TICON_STATUS_DISACTIVE,
	TICON_STATUS_ACTIVE,
 	TICON_STATUS_COUNT
}TICON_STATUS;

typedef struct _Ticon{
	TICON_STATUS status;
	GooCanvasItem *icons[TICON_STATUS_COUNT];
}Ticon;

typedef struct _Taskbar{

	gdouble x;
	gdouble y;
	gdouble width;
	gdouble height; 
	
	GooCanvasItem* group;
	GooCanvasItem* bg;
	Ticon *ticon[TASKBAR_MEMBER_COUNT];
}Taskbar;

//////////////////////////////////////////
//static func

static gboolean taskbar_ticon_create(xmlNode *a_node,Layout *layout)
{
	Taskbar *taskbar=layout->private;	
	SMS *sms=layout->sms;
	Ticon *ticon;
	GdkPixbuf *pixbuf;
	char *val,buf[256];
	int id,x,y,w,h;
	
	if(!a_node)
		return FALSE;
	
	id=sms_get_xml_int(a_node,"id",0);
	if(taskbar->ticon[id])
		return FALSE;
	
	ticon=(Ticon *)g_malloc0(sizeof(Ticon)+1);
	
	x=sms_get_xml_int(a_node,"x",0);
	y=sms_get_xml_int(a_node,"y",0);
	w=sms_get_xml_int(a_node,"w",0);
	h=sms_get_xml_int(a_node,"h",0);
	
	if(w==0||h==0)
	{
		h=taskbar->height;
		w=-1;
	}
	
	val = xmlGetProp (a_node,(xmlChar*)"disactive");
	if(val)
	{
		sprintf(buf,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,sms->theme->name,layout->name,val);
		pixbuf=sms_load_pic(buf,w,h);
		ticon->icons[TICON_STATUS_DISACTIVE]=goo_canvas_image_new(layout->cavas,pixbuf,x,y,NULL);
	}
#if 0
	val = xmlGetProp (a_node,(xmlChar*)"active");
	if(val)
	{
		sprintf(buf,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,sms->theme->name,layout->name,val);
		pixbuf=sms_load_pic(buf,w,h);
		ticon->icons[TICON_STATUS_ACTIVE]=goo_canvas_image_new(layout->cavas,pixbuf,x,y,NULL);
	}
#endif
	taskbar->ticon[id]=ticon;
	return TRUE;
}

static gboolean taskbar_bg_create(xmlNode *a_node,Layout *layout)
{
	Taskbar *taskbar=layout->private;	
	SMS *sms=layout->sms;
	Ticon *ticon;
	GdkPixbuf *pixbuf;
	char *val,buf[256];

	if(!a_node)
		return FALSE;

	taskbar->x=(gdouble)sms_get_xml_int(a_node,"x",0);
	taskbar->y=(gdouble)sms_get_xml_int(a_node,"y",0);
	taskbar->width=(gdouble)sms_get_xml_int(a_node,"w",0);
	taskbar->height=(gdouble)sms_get_xml_int(a_node,"h",0);
	
	if(taskbar->width==0||taskbar->height==0)
	{
		fprintf(stderr,"%s:Taskbar get size error.w=%f,h=%f\n",
				__FUNCTION__,taskbar->width,taskbar->height);
		return FALSE;
	}

	val = xmlGetProp (a_node,(xmlChar*)"pic");
	if(!val)
		return FALSE;

	g_object_set(layout->cavas,"x",taskbar->x,"y",taskbar->y,
				 "width",taskbar->width,"height",taskbar->height,NULL);	
	
	sprintf(buf,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,sms->theme->name,layout->name,val);
	pixbuf=sms_load_pic(buf,taskbar->width,taskbar->height);
	taskbar->bg=goo_canvas_image_new(layout->cavas,pixbuf,0,0,NULL);
	
	//free memory
	g_object_unref(pixbuf);
	return TRUE;
}

static gboolean taskbar_travel_xml(xmlNode *a_node,Layout *layout)
{
	Taskbar *taskbar=layout->private;
	char *val;
	xmlNode *cur_node = NULL;
	int icons=0;
	
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"taskbar")==0)
			{
				if(!taskbar_bg_create(cur_node,layout))
					return FALSE;	
				
				cur_node=cur_node->children;
				continue;
			}
			else if(strcmp(cur_node->name,"icon")==0)
			{
				if(taskbar_ticon_create(cur_node,layout))
					icons++;
			}
		}
	}

	if(icons==TASKBAR_MEMBER_COUNT)
		return TRUE;
	
	return FALSE;
}

static gboolean taskbar_load_config(Layout *layout)
{
	Taskbar *taskbar=layout->private;
	SMS *sms=layout->sms;
	
	gboolean ret=FALSE;
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;
	
	char conf[256];
	
	sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,sms->theme->name,layout->name);
	
	doc = xmlReadFile(conf, NULL, 0);

	if (doc == NULL) {
		fprintf(stderr,"error: could not parse file %s\n", conf);
		return ret;
	}

	root_element = xmlDocGetRootElement(doc);
	
	if(root_element)
	{
		xmlNode *cur_node = NULL;
		for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				ret=TRUE;
				break;
			}
		}
		if(ret)
			ret=taskbar_travel_xml(cur_node->children,layout);
	}

	xmlFreeDoc(doc);
	xmlCleanupParser();
	return ret;
}

static gboolean taskbar_init(Layout *layout,gpointer data)
{
	Taskbar *taskbar=(Taskbar *)g_malloc0(sizeof(Taskbar)+1);
	
	layout->private=(void *)taskbar;

	layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

	if(!taskbar_load_config(layout))
	{
		fprintf(stderr,"%s:load config err\n",__FUNCTION__);
		return FALSE;//exit(-1);
	}
	return TRUE;
}

static gboolean taskbar_clear(Layout *layout,gpointer data)
{
	return TRUE;
}

static gboolean taskbar_key_press(Layout * layout,GdkEventKey *event)
{

	return FALSE;
}

static gboolean taskbar_show_cb(Layout * layout,gpointer data)
{
	return TRUE;
}

Layout *taskbar_layout_new(GooCanvasItem * parent,gpointer sms)
{
	Layout *layout=layout_new( parent,LAYOUT_TYPE_ITEM,LAYOUT_ITEM_TASKBAR,
							   TASKBAR_LAYOUT_NAME,sms);

	layout_set_func(layout,"init",taskbar_init);
	layout_set_func(layout,"clear",taskbar_clear);
	layout_set_func(layout,"key-press",taskbar_key_press);
	layout_set_func(layout,"show",taskbar_show_cb);
	return layout;
}
